package whf.framework.tools.generator.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.resource.db.ConnectionManager;
import whf.framework.tools.generator.cfg.Config;

/**
 * @author wanghaifeng
 *
 */
public abstract class AbstractTableGetter {

	private static Log log = LogFactory.getLog(AbstractTableGetter.class);
	
	protected Config config;
	
	private ConnectionManager connectionManager;

	public AbstractTableGetter(Config config){
		this.config = config;
		log.info(this.config);
	}
	
	public ConnectionManager getConnectionManager(){
		if(this.connectionManager == null){
			Map<String, String> props = new HashMap<String, String>();
			props.put("","");
			connectionManager = new ConnectionManager(config.getDriver(), config.getUrl(), config.getUsername(), config.getPassword(), props);
		}
		return this.connectionManager;
	}
	
	public Connection getConnection() throws Exception{
//		Class.forName(this.config.getDriver());
//		return DriverManager.getConnection(this.config.getUrl(), this.config.getUsername(), this.config.getPassword());
		return this.getConnectionManager().getPooledConnection();
	}
	
	public void close(Object obj){
		if(obj == null) return;
		try{
			if(obj instanceof Connection){
				((Connection)obj).close();
			}
			if(obj instanceof Statement){
				((Statement)obj).close();
			}
			if(obj instanceof ResultSet){
				((ResultSet)obj).close();
			}
		}catch(Exception e){
			//
		}
	}

	public abstract List<Table> getTables() throws Exception;

	public abstract List<Table> getTables(String tableNames) throws Exception;
}
